package sp.phone.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.TextView;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import gov.anzong.androidnga.R;
import sp.phone.common.FilterKeyword;
import sp.phone.view.RecyclerViewEx;

public class FilterKeywordsAdapter extends RecyclerViewEx.Adapter<FilterKeywordsAdapter.FilterKeywordsViewHolder> {
    private final Context mContext;
    private final List<FilterKeyword> mKeywords;
    private View.OnClickListener mOnClickListener;

    public FilterKeywordsAdapter(Context context, List<FilterKeyword> keywords) {
        this.mContext = context;
        this.mKeywords = keywords;
    }

    @NonNull
    @Override
    public FilterKeywordsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View convertView = LayoutInflater.from(mContext).inflate(R.layout.list_filter_keywords_item, viewGroup, false);
        FilterKeywordsAdapter.FilterKeywordsViewHolder viewHolder = new FilterKeywordsAdapter.FilterKeywordsViewHolder(convertView);

        viewHolder.itemView.setOnClickListener(mOnClickListener);
        viewHolder.checkView.setOnClickListener(mOnClickListener);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull FilterKeywordsViewHolder viewHolder, int position) {
        FilterKeyword keyword = mKeywords.get(position);
        viewHolder.filterKeywordView.setText(keyword.getKeyword());
        viewHolder.checkView.setChecked(keyword.isEnabled());
        viewHolder.filterKeywordView.setTag(position);
        viewHolder.checkView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return mKeywords.size();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.mOnClickListener = onClickListener;
    }

    public static class FilterKeywordsViewHolder extends RecyclerView.ViewHolder {

        TextView filterKeywordView;
        Switch checkView;

        public FilterKeywordsViewHolder(@NonNull View itemView) {
            super(itemView);
            filterKeywordView = itemView.findViewById(R.id.filter_keyword);
            checkView = itemView.findViewById(R.id.check);
        }
    }
}